using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// pool manager template
public class PoolMgr<T> : Singleton<PoolMgr<T>> where T : MonoBehaviour
{
    // pool
    private Dictionary<string, Queue<T>> _pool = new Dictionary<string, Queue<T>>();
    // get object from pool
    public T Get(string name)
    {
        if (_pool.ContainsKey(name))
        {
            if (_pool[name].Count > 0)
            {
                return _pool[name].Dequeue();
            }
        }
        return null;
    }
    // put object to pool
    public void Put(string name, T obj)
    {
        if (_pool.ContainsKey(name))
        {
            _pool[name].Enqueue(obj);
        }
        else
        {
            _pool.Add(name, new Queue<T>());
            _pool[name].Enqueue(obj);
        }
    }

    public void Clean(string name)
    {
        CleanPool(name);
    }

    private void CleanPool(string name){
        if (_pool.ContainsKey(name))
        {
            //遍历列队
            foreach (var item in _pool[name])
            {
                //销毁对象
                LoadMgr.Instance.ReleaseInstance(item.gameObject);
            }
            _pool[name].Clear();
        }
    }

    // clear pool
    public override void Clean()
    {
        foreach (var item in _pool)
        {
            CleanPool(item.Key);
        }
    }
}
